package com.example.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Person;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.PersonMapper;
import com.example.springboot.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/person")
public class PersonController {

    @Resource
    PersonMapper personMapper;
    @Resource
    UserMapper userMapper;

    @PostMapping("/find")
    public Result<?> findPerson(@RequestBody User user) {
        Person person = personMapper.selectOne(Wrappers.<Person>lambdaQuery().eq(Person::getUid,user.getUid()));
        if (person == null) {
            return Result.error("-1","查询Person信息错误：无uid的person");
        }
        return Result.success(person);
    }
    @PostMapping("/update")
    public Result<?> updatePerson(@RequestBody Map<String,Object> map) {
        String personstr = JSONObject.toJSONString(map.get("person"));
        String userstr = JSONObject.toJSONString(map.get("user"));
        Person person = JSONObject.parseObject(personstr,Person.class);
        User user =  JSONObject.parseObject(userstr,User.class);
//        System.out.println(map.toString());;
//        Person person = (Person) map.get("person");
//        User user = (User) map.get("user");
        personMapper.updateById(person);
        userMapper.updateById(user);
        return Result.success();
    }
    @GetMapping("/password")
    public Result<?> updatePerson(@RequestParam("id") Integer id) {
        User user = userMapper.selectById(id);
        return Result.success(user);
    }

}
